﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using System.Threading.Tasks;

namespace FrogEngine
{
	public class CameraService : GameEntityServiceBase<ICamera>, ICameraService
	{
		public CameraService(IEngine engine)
			: base(engine)
		{
			serviceOrder = 1;
		}
		
		public override bool Subscribe(IGameEntity entity)
		{
			if (base.Subscribe(entity))
			{
				return true;
			}
			else
				return false;
		}

		public override bool Unsubscribe(IGameEntity entity)
		{
			if (base.Unsubscribe(entity))
			{
				ICamera cam = entity as ICamera;
				if (activeCamera == cam)
					activeCamera = (ICamera)subscribedEntities.FirstOrDefault(a => a != cam);
				return true;
			}
			else
				return false;
		}

		private ICamera activeCamera;
		public ICamera GetActiveCamera()
		{
			return activeCamera;
		}

		public void SetActiveCamera(ICamera camera)
		{
			if (subscribedEntities.Contains(camera))
				activeCamera = camera;
		}

		public void Update(GameTime gameTime)
		{
			Parallel.ForEach(subscribedEntities, a => (a as ICamera).Update(gameTime));
		}
	}
}
